<?php
/**
 * Created by PhpStorm.
 * User: server
 * Date: 2018/9/10
 * Time: 16:28
 */

namespace app\good\controller;

use app\common\controller\Base;
use think\Db;
use tools\curl\Curl;

/**
 * @title 商品SKU（04-02）
 * @description 接口说明
 * @group 商品（04）
 * @header name:model_id require:0 default:04 desc:秘钥(区别设置)
 * @groupremark 已使用错误码1040001
 */
class FrontGoodSku extends Base
{

    /**
     * @title 添加Sku属性
     * @description 接口说明
     * @author 开发者（李锐志）
     * @url /good/front_good_sku/add_good_attr
     * @method POST
     * @param name:good_id type:int require:0 default:0 other: desc:商品id
     * @param name:attr_name type:int require:0 default:0 other: desc:属性名称
     * @return id:属性id
     * @return good_id:商品id
     * @return attr_name:属性名称
     */
    public function add_good_attr()
    {

        $data = input();

        $data['sys_sign'] = SYSSIGN;

        $result = Db::name('good_attr')
            ->insert([
                'good_id' => $data['good_id'],
                'attr_name' => $data['attr_name'],
                'sys_sign' => $data['sys_sign']
            ]);

        $good_attr_id = Db::name('good_attr')
            ->getLastInsID();

        if ($result) {
            $msg['msg'] = '添加成功';
            $msg_data = Db::name('good_attr')
                ->where('id', $good_attr_id)
                ->field('sys_sign', true)
                ->find();
            $msg['data'] = $msg_data;
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-04-02-0001';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除Sku属性
     * @description 接口说明
     * @author 开发者（李锐志）
     * @url /good/front_good_sku/del_good_attr
     * @method POST
     * @param name:attr_id type:int require:0 default:0 other: desc:属性id
     */
    public function del_good_attr()
    {

        $data = input();

        $result = Db::name('good_attr')
            ->where('id', $data['attr_id'])
            ->delete();

        //连同属性值一起删除
        Db::name('good_attr_item')
            ->where('good_attr_id', $data['attr_id'])
            ->delete();

        if ($result) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-04-02-0003';
            $msg['msg'] = '删除失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 添加Sku属性值
     * @description 接口说明
     * @author 开发者（李锐志）
     * @url /good/front_good_sku/add_good_attr_item
     * @method POST
     * @param name:good_id type:int require:0 default:0 other: desc:商品id
     * @param name:good_attr_id type:int require:0 default:0 other: desc:属性值id
     * @param name:item_name type:int require:0 default:0 other: desc:属性值名称
     * @return id:属性值id
     * @return good_id:商品id
     * @return good_attr_id:属性id
     * @return item_name:属性值名称
     */
    public function add_good_attr_item()
    {

        $data = input();

        $data['sys_sign'] = SYSSIGN;

        $result = Db::name('good_attr_item')
            ->insert([
                'good_id' => $data['good_id'],
                'good_attr_id' => $data['good_attr_id'],
                'item_name' => $data['item_name'],
                'sys_sign' => $data['sys_sign']
            ]);

        $good_attr_item_id = Db::name('good_attr_item')
            ->getLastInsID();

        if ($result) {
            $msg['msg'] = '添加成功';
            $msg_data = Db::name('good_attr_item')
                ->where('id', $good_attr_item_id)
                ->field('sys_sign', true)
                ->find();
            $msg['data'] = $msg_data;
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-04-02-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除Sku属性值
     * @description 接口说明
     * @author 开发者（李锐志）
     * @url /good/front_good_sku/del_good_attr_item
     * @method POST
     * @param name:attr_item_id type:int require:0 default:0 other: desc:属性值id
     */
    public function del_good_attr_item()
    {

        $data = input();

        $result = Db::name('good_attr_item')
            ->where('id', $data['attr_item_id'])
            ->delete();

        if ($result) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-04-02-0004';
            $msg['msg'] = '删除失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 保存sku信息
     * @description 接口说明
     * @author 开发者（李锐志）
     * @url /good/front_good_sku/save_sku
     * @method POST
     * @param name:sku_message type:int require:0 default:0 other: desc:sku信息
     */
    public function save_sku()
    {

        $data = input();

        //$data['sku_message'] = '{"good_id":22,"good_attr":[{"id":2,"good_id":22,"attr_name":"颜色","good_attr_item":[{"id":3,"good_id":22,"good_attr_id":2,"item_name":"红色"},{"id":4,"good_id":22,"good_attr_id":2,"item_name":"红色"}]}],"good_sku":[{"id":1,"good_id":2,"good_attr_ids":"1,3","good_attr_item_ids":"3,5","price":150,"num":1,"img":"565465465466.jpg"}]}';

        $sku_arr = json_decode($data['sku_message'], true);

        $sys_sign = SYSSIGN;

        foreach ($sku_arr['front_front_good_sku'] as $k => $v) {

            //如果id等于0，说明数据库中没有该sku，则添加到数据库中；不为0，则更新该sku
            if ($v['id'] == 0) {

                $result = Db::name('front_good_sku')
                    ->insert([
                        'good_id' => $v['good_id'],
                        'good_attr_ids' => $v['good_attr_ids'],
                        'good_attr_item_ids' => $v['good_attr_item_ids'],
                        'price' => $v['price'],
                        'num' => $v['num'],
                        'img' => $v['img'],
                        'sys_sign' => $sys_sign
                    ]);
            } else {

                $result = Db::name('front_good_sku')
                    ->where('id', $v['id'])
                    ->update([
                        'good_id' => $v['good_id'],
                        'good_attr_ids' => $v['good_attr_ids'],
                        'good_attr_item_ids' => $v['good_attr_item_ids'],
                        'price' => $v['price'],
                        'num' => $v['num'],
                        'img' => $v['img'],
                        'sys_sign' => $sys_sign
                    ]);
            }
        }

        if ($result) {
            $msg['msg'] = '保存成功';
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-04-02-0005';
            $msg['msg'] = '保存失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 返回sku信息
     * @description 接口说明
     * @author 开发者（李锐志）
     * @url /good/front_good_sku/return_sku
     * @method POST
     * @param name:good_id type:int require:0 default:0 other: desc:商品id
     */
    public function return_sku()
    {

        $data = input();

        $result['good_id'] = $data['good_id'];

        $result['good_attr'] = Db::name('good_attr')
            ->where('good_id', $data['good_id'])
            ->field('sys_sign', true)
            ->select();

        foreach ($result['good_attr'] as $k => $v) {
            $result['good_attr'][$k]['good_attr_item'] = Db::name('good_attr_item')
                ->where('good_attr_id', $v['id'])
                ->field('sys_sign', true)
                ->select();
        }

        $result['good_sku'] = Db::name('good_sku')
            ->where('good_id', $data['good_id'])
            ->field('sys_sign', true)
            ->select();

        if ($result) {
            $msg['msg'] = '返回成功';
            $msg['data'] = $result;
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-04-02-0006';
            $msg['msg'] = '返回失败';
            return $this->errorJson($msg);
        }
    }
}